#!/bin/bash
#
# Handle the main menu for building MaxWit Linux
#
# http://www.maxwit.com
# http://maxwit.googlecode.com
#
# Authors:
#     Conke Hu    <conke.hu@gmail.com>
#     Tiger Yu    <tigerflying.yu@gmail.com>
#     Fleya Hou   <fleya.hou@gmail.com>
#


APP_TOP="${MW_TOP_DIR}/application"

BUILT_TAG=".maxwit_built"

BUILD_TOOLS_LIST="${MWP_ZLIB} ${MWP_PKG_CONFIG} ${MWP_AUTOCONF} ${MWP_AUTOMAKE}"

BuildAppPkg() # fixme for loop
{
	local df=""
	local subdir="${APP_TOP}/${1}"
	local srcdir="${APP_BUILD_PATH}/${1}"

	if [ -d "${subdir}" ]; then
		cd "${subdir}" 
	else
		echo "\"${subdir}\" does NOT exist!"
		return 1
	fi

	if [ -e "${srcdir}/${BUILT_TAG}" ]; then
		echo "\"${1}\" has been built."
		echo
		return 0
	fi

	if [ -f dep ]; then
		for df in `cat dep`
		do
			BuildAppPkg ${df} || return 1
		done
	fi	

	CheckPkg ${1}

	Unpack ${1} application

	cd ${srcdir}

	# fixme
	${SB2_CMD} ${subdir}/build.sh && \
	touch ${srcdir}/${BUILT_TAG}

	echo
	return 0
}


BuildAppList()
{
	local pkg=""

	for pkg in $*
	do
		BuildAppPkg ${pkg} || exit 1
	done
}


BuildSbox2()
{
	local SB2_CONF_PATH="${HOME}/.scratchbox2"
	local SB2_CURR_CONF="maxwit-linux"
	local dir

	echo -n "Initializing rootfs "

	for dir in `ls ${APP_BUILD_PATH}`
	do
		rm -rf ${APP_BUILD_PATH}/${dir}
		echo -n "."
	done

	rm -rf ${ROOTFS_PATH}
	mkdir -p ${ROOTFS_PATH}
	echo -n "."

	cd ${ROOTFS_PATH}

	mkdir -p etc/init.d etc/udev/rules.d bin sbin usr/bin usr/include usr/games proc sys dev root home tmp
	for i in 1 2 3 4 5 6 7 8
	do
		mkdir -p usr/share/man/man${i}
	done

	cp -a ${SYSROOT_PATH}/lib .
	echo -n "."
	cp -a ${SYSROOT_PATH}/usr/${TARGET_PLAT}/lib usr/
	cp -a ${SYSROOT_PATH}/usr/lib usr/
	echo -n "."

	if [ ${USER} = root ]; then
		[ -e ${ROOTFS_PATH}/dev/console ] || \
			mknod ${ROOTFS_PATH}/dev/console c 5 1

		[ -e ${ROOTFS_PATH}/dev/null ] || \
			mknod ${ROOTFS_PATH}/dev/null c 1 3
	else
		echo -n "(TODO: create device nodes by hand!)"
	fi

	cp /etc/group ${ROOTFS_PATH}/etc # fixme

cat > ${ROOTFS_PATH}/etc/init.d/rcS << EOF
#!/bin/sh

[ -e /dev/console ] || mknod /dev/console c 5 1

echo
echo "    ***********************************"
echo "    *    Welcome to MaxWit Linux !    *"
echo "    *    [ http://www.maxwit.com ]    *"
echo "    ***********************************"
echo

mount -t sysfs sysfs  /sys
mount -t tmpfs tmpfs  /dev
mount -t proc  proc   /proc
mount -t tmpfs tmpfs  /tmp

EOF

chmod 755 ${ROOTFS_PATH}/etc/init.d/rcS

	echo " OK."

	echo "Initializing scratchbox2:"

	rm -rf ${SB2_CONF_PATH}
	mkdir -p ${SB2_CONF_PATH}

	cp ${SRC_PATH}/${SBP_LIBTOOL}.tar.gz ${SB2_CONF_PATH} || exit 1

	cd ${ROOTFS_PATH}

	# sb2-init ${SB2_CURR_CONF} ${TARGET_PLAT}-gcc || exit 1
	SB2_EMU=`which qemu-${TARGET_ARCH}`
	if [ -z "${SB2_EMU}" ]; then
		echo "qemu-${TARGET_ARCH} NOT found in PATH:"
		echo ${PATH}
		exit 1
	else
		echo "sb2: using emulator \"${SB2_EMU}\""
	fi

	sb2-init -c ${SB2_EMU} ${SB2_CURR_CONF} ${TARGET_PLAT}-gcc || exit 1
	# ${UTILS_PATH}/bin/sb2-config -d ${SB2_CURR_CONF} || exit 1
	echo


	# bash as a test case :)
	Unpack ${MWP_BASH} application

	sb2 ./configure \
		--prefix=/usr \
		--exec-prefix=/ \
		--sysconfdir=/etc \
		|| exit 1

	sb2 make || exit 1

	sb2 -m install make install || exit 1

	ln -sf bash ${ROOTFS_PATH}/bin/sh || exit 1
	cp -v ${APP_TOP}/${MWP_BASH}/bashrc ${ROOTFS_PATH}/root/.bashrc && \
	cp -v ${APP_TOP}/${MWP_BASH}/bash_profile ${ROOTFS_PATH}/root/.bash_profile
}


BuildBusybox()
{
	local BB_OPT="ARCH=arm CROSS_COMPILE=${TARGET_PLAT}- CONFIG_PREFIX=${ROOTFS_PATH}"

	Unpack ${MWP_BUSYBOX} application || exit 1

	cp ${APP_TOP}/${MWP_BUSYBOX}/defconfig ${APP_BUILD_PATH}/${MWP_BUSYBOX}/scripts/ && \
	make ${BB_OPT} defconfig || exit 1

	make ${BB_OPT} install V=1 || exit 1
}


BuildSysCore1()
{
	local pkg_list="${MWP_BUSYBOX} ${BUILD_TOOLS_LIST} ${MWP_UDEV}"

 	BuildSbox2 || exit 1
 
 	for pkg in ${pkg_list}
 	do
 		rm -rf ${APP_BUILD_PATH}/${pkg} 
 	done

	BuildAppList ${pkg_list}
}


BuildSysCore2()
{
	local pkg_list="\
				${MWP_SYSVINIT} \
				${MWP_COREUTILS} \
				${MWP_UTIL_LINUX} \
				${BUILD_TOOLS_LIST} \
				${MWP_UDEV} \
				${MWP_MODULE_INIT} \
				"

	BuildSbox2 || exit 1

	for pkg in ${pkg_list}
	do
		rm -rf ${APP_BUILD_PATH}/${pkg}
	done

	BuildAppList ${pkg_list}
}


RunQemu()
{
	QEMU_CONSOLE=0
	QEMU_MACH="realview"
	QEMU_HOST_IP="10.0.0.1"
	QEMU_MACH_IP="10.0.0.2"

	echo '#!/bin/sh' > /etc/qemu-ifup
	echo "/sbin/ifconfig \$1 ${QEMU_HOST_IP}" >> /etc/qemu-ifup
	chmod a+x /etc/qemu-ifup

	echo '#!/bin/sh' > /etc/qemu-ifdown
	echo "/sbin/ifconfig \$1 down" >> /etc/qemu-ifdown
	chmod a+x /etc/qemu-ifdown

	if [ x"${QEMU_CONSOLE}" != x"1" ]; then
		EXTRA_QEMU_OPT="-serial stdio"
		EXTRA_COMMAND_LINE="console=ttyAMA0" # fixme
	fi

	qemu-system-${TARGET_ARCH} -M ${QEMU_MACH} -kernel ${IMAGES_PATH}/zImage.${QEMU_MACH} -net nic -net tap ${EXTRA_QEMU_OPT} -append "root=/dev/nfs rw nfsroot=${QEMU_HOST_IP}:${ROOTFS_PATH} ip=${QEMU_MACH_IP}:${QEMU_HOST_IP}:${QEMU_HOST_IP}:255.255.255.0:maxwit.googlecode.com:eth0:off ${EXTRA_COMMAND_LINE}"
}


BuildImage()
{
	local IMAGE_PREFIX=rootfs

	for imgtool in mkyaffsimage mkyaffs2image mkfs.jffs2 mkfs.cramfs cpio
	do
		TOOL_PATH=`which ${imgtool}`
		case "$TOOL_PATH" in
		*mkyaffsimage)
			echo "yaffs1 image (${IMAGES_PATH}/${IMAGE_PREFIX}.yaffs1) ... "
			${TOOL_PATH} ${ROOTFS_PATH} ${IMAGES_PATH}/${IMAGE_PREFIX}.yaffs1 > /dev/null
			chmod 0644 ${IMAGES_PATH}/${IMAGE_PREFIX}.yaffs1
			;;	
		*mkyaffs2image)
			echo -n "yaffs2 image (${IMAGES_PATH}/${IMAGE_PREFIX}.yaffs2) ... "
			${TOOL_PATH} ${ROOTFS_PATH} ${IMAGES_PATH}/${IMAGE_PREFIX}.yaffs2 > /dev/null
			chmod 0644 ${IMAGES_PATH}/${IMAGE_PREFIX}.yaffs2
			;;
		*mkfs.jffs2)
			echo -n "jffs2 image (${IMAGES_PATH}/${IMAGE_PREFIX}.jffs2) ... "
			${TOOL_PATH} -r ${ROOTFS_PATH} -s 512 -e 0x4000 -l -p -n -o ${IMAGES_PATH}/${IMAGE_PREFIX}_s.jffs2
			${TOOL_PATH} -r ${ROOTFS_PATH} -s 2048 -e 0x20000 -l -p -n -o ${IMAGES_PATH}/${IMAGE_PREFIX}_l.jffs2
			;;	
		*mkfs.cramfs)
			echo "cramfs image (${IMAGES_PATH}/${IMAGE_PREFIX}.cramfs) ... "
			${TOOL_PATH} -b 512 ${ROOTFS_PATH} ${IMAGES_PATH}/${IMAGE_PREFIX}.cramfs
			;;
		*cpio)
			echo "ramdisk image (${IMAGES_PATH}/${IMAGE_PREFIX}.rd) ..."
			cd ${ROOTFS_PATH} && find | ${TOOL_PATH} -o -H newc 2> /dev/null | gzip -9 > ${IMAGES_PATH}/${IMAGE_PREFIX}.rd
			;;
		*)
			echo "${imgtool} not found, skipping"
			;;
		esac
	done
}


